package com.alan.base.view;

import android.os.Handler;
import android.os.Looper;

import androidx.annotation.NonNull;

import com.alan.base.utils.Logger;

public class AutoDismissHandler {

    private final long timeAfter;
    private final Handler handler;
    private final Runnable runnable;
    private final String TAG = getClass().getSimpleName();

    public AutoDismissHandler(long time, @NonNull Runnable runnable) {
        Logger.d(TAG, "init");
        this.timeAfter = time;
        this.runnable = runnable;
        handler = new Handler(Looper.getMainLooper());
        start();
    }

    public void pause() {
        Logger.d(TAG, "pause");
        handler.removeCallbacks(runnable);
    }

    public void start() {
        Logger.d(TAG, "start");
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, timeAfter);
    }

    public void release() {
        Logger.d(TAG, "release");
        handler.removeCallbacks(runnable);
    }
}
